<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智慧校园 - 请假审核</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4a90e2;
            --success-color: #2ecc71;
            --danger-color: #e74c3c;
            --text-color: #2c3e50;
            --border-color: #dcdde1;
            --background-color: #f5f6fa;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            background: var(--background-color);
            color: var(--text-color);
            margin: 0;
            padding: 20px;
        }

        .dashboard {
            max-width: 1400px;
            margin: 0 auto;
        }

        /* 统计卡片样式 */
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            transition: transform 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
        }

        .stat-card i {
            font-size: 2.5em;
            color: var(--primary-color);
            margin-right: 20px;
        }

        .stat-info h3 {
            margin: 0;
            font-size: 0.9em;
            color: #666;
        }

        .stat-info p {
            margin: 5px 0 0;
            font-size: 1.8em;
            font-weight: bold;
            color: var(--primary-color);
        }

        /* 表格样式 */
        .table-responsive {
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
            margin-bottom: 20px;
            padding: 20px;
        }

        .table {
            width: 100%;
            border-collapse: collapse;
        }

        .table th {
            background: #f8f9fa;
            padding: 15px;
            text-align: left;
            font-weight: 600;
            color: var(--text-color);
            border-bottom: 2px solid var(--border-color);
        }

        .table td {
            padding: 15px;
            border-top: 1px solid var(--border-color);
            vertical-align: middle;
        }

        .table tr:hover {
            background-color: #f8f9fa;
        }

        /* 按钮样式 */
        .btn-group {
            display: flex;
            gap: 8px;
        }

        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-weight: 500;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            transition: all 0.3s ease;
        }

        .btn-success {
            background: var(--success-color);
            color: white;
        }

        .btn-danger {
            background: var(--danger-color);
            color: white;
        }

        .btn:hover {
            opacity: 0.9;
            transform: translateY(-1px);
        }

        /* 加载动画 */
        .loading-spinner {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255,255,255,0.8);
            z-index: 2000;
            justify-content: center;
            align-items: center;
        }

        .spinner {
            width: 50px;
            height: 50px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid var(--primary-color);
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .table-responsive {
                overflow-x: auto;
            }

            .btn-group {
                flex-direction: column;
            }

            .btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
<div class="dashboard">
    <!-- 顶部统计卡片 -->
    <div class="stats-container">
        <div class="stat-card">
            <i class="fas fa-clipboard-list"></i>
            <div class="stat-info">
                <h3>待审核申请</h3>
                <p id="pendingCount">0</p>
            </div>
        </div>
        <div class="stat-card">
            <i class="fas fa-check-circle"></i>
            <div class="stat-info">
                <h3>已审核申请</h3>
                <p id="approvedCount">0</p>
            </div>
        </div>
        <div class="stat-card">
            <i class="fas fa-clock"></i>
            <div class="stat-info">
                <h3>今日申请</h3>
                <p id="todayCount">0</p>
            </div>
        </div>
    </div>

    <!-- 数据表格 -->
    <div class="table-responsive">
        <table id="leaveTable" class="table">
            <thead>
            <tr>
                <th>请假编号</th>
                <th>学号</th>
                <th>课程编号</th>
                <th>请假事由</th>
                <th>请假天数</th>
                <th>申请时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <!-- 数据将通过JS动态加载 -->
            </tbody>
        </table>
    </div>
</div>

<!-- 加载动画 -->
<div id="loadingSpinner" class="loading-spinner">
    <div class="spinner"></div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        // 显示/隐藏加载动画
        function showLoading(show) {
            if (show) {
                $("#loadingSpinner").css("display", "flex");
            } else {
                $("#loadingSpinner").hide();
            }
        }

        // 发送Ajax请求加载请假单列表
        showLoading(true);
        $.ajax({
            url: '/attend_war/audit',
            type: 'GET',
            dataType: 'json',
            success: function(data) {
                var tableContent = '';
                if (data && Array.isArray(data) && data.length > 0) {
                    $.each(data, function(index, leave) {
                        tableContent += '<tr>';
                        tableContent += '<td>' + leave.leaveID + '</td>';
                        tableContent += '<td>' + leave.stuNo + '</td>';
                        tableContent += '<td>' + leave.courseID + '</td>';
                        tableContent += '<td>' + leave.reason + '</td>';
                        tableContent += '<td>' + leave.daynum + '</td>';
                        tableContent += '<td>' + leave.applytime + '</td>';
                        tableContent += '<td class="btn-group">';
                        tableContent += '<button class="btn btn-success approveBtn" data-id="' + leave.leaveID + '" data-status="1"><i class="fas fa-check"></i> 同意</button>';
                        tableContent += '<button class="btn btn-danger approveBtn" data-id="' + leave.leaveID + '" data-status="2"><i class="fas fa-times"></i> 不同意</button>';
                        tableContent += '</td>';
                        tableContent += '</tr>';
                    });

                    // 更新统计信息
                    $("#pendingCount").text(data.length);
                    $("#approvedCount").text("--");
                    $("#todayCount").text("--");
                } else {
                    tableContent = '<tr><td colspan="7" class="text-center">没有请假记录</td></tr>';
                }
                $('#leaveTable tbody').html(tableContent);
                showLoading(false);
            },
            error: function(xhr, status, error) {
                alert('加载请假申请失败: ' + error);
                showLoading(false);
            }
        });

        // 审核按钮点击事件
        $(document).on('click', '.approveBtn', function() {
            var leaveId = $(this).data('id');
            var status = $(this).data('status');

            showLoading(true);
            $.ajax({
                url: '/attend_war/audit',
                type: 'POST',
                data: {
                    leaveId: leaveId,
                    status: status
                },
                dataType: 'json',
                success: function(response) {
                    if (response.success) {
                        alert('审核成功');
                        location.reload();
                    } else {
                        alert('审核失败: ' + response.message);
                    }
                    showLoading(false);
                },
                error: function(jqXHR, textStatus) {
                    alert("审核失败: " + textStatus);
                    showLoading(false);
                }
            });
        });
    });
</script>
</body>
</html>